<template>
	<div>
		<el-row :gutter="20">
			<el-col :span="24">
				<el-card shadow="hover">
					<template #header>
						<div class="clearfix">
							<span>修改密码</span>
						</div>
					</template>
					<el-row>
						<el-col :span="12">
							<el-form label-width="120px">
								<el-form-item label="账号(手机号码)">
									<el-input v-model="phone" readonly></el-input>
								</el-form-item>
								<el-form-item label="原密码">
									<el-input type="password" v-model="form.oldPwd"></el-input>
								</el-form-item>
								<el-form-item label="新密码">
									<el-input type="password" v-model="form.newPwd"></el-input>
								</el-form-item>
								<el-form-item label="确认密码">
									<el-input type="password" v-model="form.rePwd"></el-input>
								</el-form-item>
								<el-form-item>
									<el-button type="primary" @click="onSubmit">确认</el-button>
								</el-form-item>
							</el-form>
						</el-col>
					</el-row>
				</el-card>
			</el-col>
		</el-row>
	</div>
</template>

<script setup lang="ts" name="user">
import { reactive, ref } from 'vue';
import {ElMessage} from "element-plus";
import {passwordApi} from "../api/index";
import { useRouter } from 'vue-router';

const router = useRouter();
const phone = localStorage.getItem('phone');
const form = reactive({
	oldPwd: '',
	newPwd: '',
	rePwd: ''
});
const onSubmit = async () => {
	if (!form.oldPwd) {
		ElMessage.warning("请输入原密码");
		return;
	}
	if (!form.newPwd) {
		ElMessage.warning("请输入新密码");
		return;
	}
	if (!form.rePwd) {
		ElMessage.warning("请输入确认密码");
		return;
	}
	if (form.newPwd != form.rePwd) {
		ElMessage.warning("两次密码不一致");
		return;
	}
	const res = await passwordApi({
		oldPassword: form.oldPwd,
		newPassword: form.newPwd
	});
	if (res) {
		ElMessage.success("修改成功，请重新登录");
		localStorage.clear();
		await router.push("/login")
	}
};

</script>

<style scoped>
.info {
	text-align: center;
	padding: 35px 0;
}
.info-image {
	position: relative;
	margin: auto;
	width: 100px;
	height: 100px;
	background: #f8f8f8;
	border: 1px solid #eee;
	border-radius: 50px;
	overflow: hidden;
}

.info-edit {
	display: flex;
	justify-content: center;
	align-items: center;
	position: absolute;
	left: 0;
	top: 0;
	width: 100%;
	height: 100%;
	background: rgba(0, 0, 0, 0.5);
	opacity: 0;
	transition: opacity 0.3s ease;
}
.info-edit i {
	color: #eee;
	font-size: 25px;
}
.info-image:hover .info-edit {
	opacity: 1;
}
.info-name {
	margin: 15px 0 10px;
	font-size: 24px;
	font-weight: 500;
	color: #262626;
}
.crop-demo-btn {
	position: relative;
}
.crop-input {
	position: absolute;
	width: 100px;
	height: 40px;
	left: 0;
	top: 0;
	opacity: 0;
	cursor: pointer;
}
</style>
